#include "Player.h"


CPlayer::CPlayer()
{
	currentStatus_ = JUMP;
	currentBullet_ = NULL;
	loadSprites();
	velocity_.x = 0;
	velocity_.y = 0;
	position_.x = 200;
	position_.y = 50;
	position_.z = 0.5;
	isJumped = false;
}


CPlayer::~CPlayer()
{
}

void CPlayer::loadSprites()
{
	spriteVector_.push_back(new CSprite(L"Player//RIGHT_STAND.png", 1, 2, 2));
	spriteVector_.push_back(new CSprite(L"Player//RIGHT_WALK.png", 1, 5, 5));
	spriteVector_.push_back(new CSprite(L"Player//RIGHT_JUMP.png", 1, 4, 4));
	spriteVector_.push_back(new CSprite(L"Player//RIGHT_SWIM.png", 1, 1, 1));
	spriteVector_.push_back(new CSprite(L"Player//RIGHT_DIE.png", 1, 4, 4));
	spriteVector_.push_back(new CSprite(L"Player//RIGHT_SHOOT.png", 1, 1, 1));
	spriteVector_.push_back(new CSprite(L"Player//RIGHT_SHOT_UP.png", 1, 2, 2));
	spriteVector_.push_back(new CSprite(L"Player//RIGHT_SHOT_UP45.png", 1, 3, 3));
	spriteVector_.push_back(new CSprite(L"Player//RIGHT_SHOT_DOWN45.png", 1, 3, 3));
	spriteVector_.push_back(new CSprite(L"Player//RIGHT_SHOT_DOWN.png", 1, 1, 1));
	spriteVector_.push_back(new CSprite(L"Player//LEFT_STAND.png", 1, 2, 2));
	spriteVector_.push_back(new CSprite(L"Player//LEFT_WALK.png", 1, 5, 5));
	spriteVector_.push_back(new CSprite(L"Player//LEFT_JUMP.png", 1, 4, 4));
	spriteVector_.push_back(new CSprite(L"Player//LEFT_SWIM.png", 1, 1, 1));
	spriteVector_.push_back(new CSprite(L"Player//LEFT_DIE.png", 1, 4, 4));
	spriteVector_.push_back(new CSprite(L"Player//LEFT_SHOOT.png", 1, 1, 1));
	spriteVector_.push_back(new CSprite(L"Player//LEFT_SHOT_UP.png", 1, 2, 2));
	spriteVector_.push_back(new CSprite(L"Player//LEFT_SHOT_UP45.png", 1, 3, 3));
	spriteVector_.push_back(new CSprite(L"Player//LEFT_SHOT_DOWN45.png", 1, 3, 3));
	spriteVector_.push_back(new CSprite(L"Player//LEFT_SHOT_DOWN.png", 1, 1, 1));
}

void CPlayer::Update(DWORD deltaTime)
{
	position_.x += velocity_.x * deltaTime;
	position_.y += velocity_.y * deltaTime;
	velocity_.y = velocity_.y + 0.1f;
	if (isLanded())
		velocity_.y = 0;
	if (this->isJumped)
		currentStatus_ = JUMP;
	this->isJumped = false;
}

void CPlayer::Render()
{
	spriteVector_[currentStatus_]->Render(0, -1, position_);
}

void CPlayer::setDirection(Direction to)
{
	if (to == Direction::LEFT)
		this->velocity_.x = this->velocity_.x > 0 ? -this->velocity_.x : -0.5;
	else
		if (to == Direction::RIGHT)
			this->velocity_.x = this->velocity_.x < 0 ? -this->velocity_.x : 0.5;
		else
			if (to == Direction::UP)
				this->velocity_.y = this->velocity_.y > 0 ? -this->velocity_.y : this->velocity_.y;
			else
				this->velocity_.y = this->velocity_.y < 0 ? -this->velocity_.y : this->velocity_.y;

}

void CPlayer::setStatus(PlayerStatus status)
{
	if (currentStatus_ != status )
	{
		currentStatus_ = status;
	}
}

void CPlayer::UpdateKeyBoard(CInput* inputDevice)
{
	if (this->velocity_.x == 0)
	{
		if (isLanded())
			if (currentStatus_ != CONTRA_LEFT(STAND))
				this->setStatus(currentStatus_ > NUMBER_OF_STATUS ? CONTRA_LEFT(STAND) : STAND);
	}
	if (inputDevice->KeyDown(DIK_SPACE))
	{
		this->isJumped = true;
		if (currentStatus_ < PlayerStatus::NUMBER_OF_STATUS || inputDevice->KeyDown(DIK_RIGHTARROW) )
			this->setStatus(PlayerStatus::JUMP);
		if (currentStatus_ >= PlayerStatus::NUMBER_OF_STATUS || inputDevice->KeyDown(DIK_LEFTARROW) )
			this->setStatus(CONTRA_LEFT(PlayerStatus::JUMP));
		if (isLanded())
			this->velocity_.y = -1.5;
	}
		if (inputDevice->KeyDown(DIK_LEFTARROW))
		{
			this->velocity_.x = -0.5f;
			if (isLanded()) {
				if (inputDevice->KeyDown(DIK_UPARROW))
					this->setStatus(CONTRA_LEFT(PlayerStatus::SHOOT_UP45));
				else
					if (inputDevice->KeyDown(DIK_DOWNARROW))
						this->setStatus(CONTRA_LEFT(PlayerStatus::SHOOT_DOWN45));
					else
						this->setStatus(CONTRA_LEFT(WALK));
			}
		}
		else
			if (inputDevice->KeyDown(DIK_RIGHTARROW))
			{
				this->velocity_.x = 0.5f;
				if (isLanded()) {
					if (inputDevice->KeyDown(DIK_UPARROW))
						this->setStatus(PlayerStatus::SHOOT_UP45);
					else
						if (inputDevice->KeyDown(DIK_DOWNARROW))
							this->setStatus(PlayerStatus::SHOOT_DOWN45);
						else
							this->setStatus(WALK);
				}
			}
			else
				if (inputDevice->KeyPress(DIK_DOWNARROW))
				{
					if (isLanded()) {
						if (currentStatus_ < PlayerStatus::NUMBER_OF_STATUS)
							this->setStatus(PlayerStatus::SHOOT_DOWN);
						if (currentStatus_ > PlayerStatus::NUMBER_OF_STATUS)
							this->setStatus(CONTRA_LEFT(PlayerStatus::SHOOT_DOWN));
						this->velocity_.x = 0;
					}
				}
				else
					if (inputDevice->KeyPress(DIK_UPARROW))
					{
						if (isLanded()) {
							if (currentStatus_ < PlayerStatus::NUMBER_OF_STATUS)
								this->setStatus(PlayerStatus::SHOOT_UP);
							if (currentStatus_ > PlayerStatus::NUMBER_OF_STATUS)
								this->setStatus(CONTRA_LEFT(PlayerStatus::SHOOT_UP));
							this->velocity_.x = 0;
						}
					}
					else
					{
						if (isLanded()) {
							if (this->velocity_.x > 0)
								this->setStatus(PlayerStatus::STAND);
							if (this->velocity_.x < 0)
								this->setStatus(CONTRA_LEFT(PlayerStatus::STAND));
							this->velocity_.x = 0;
						}
					}
	
}

bool CPlayer::isLanded()
{
	if (this->position_.y > 62)
		return true;
	return false;
}